package convertidor;

import java.util.List;

import javax.el.ELContext;
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import model.Cliente;


@FacesConverter("convertidor.ClienteConverter2")
public class ClienteSinUSRConverter implements Converter{

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		
		System.out.println("Convirtiendo de "+value);

		List<Cliente> clientes= this.getDAO();
		for(Cliente c : clientes){
			if (c.getCliCedula().equals(value))
			{
				System.out.println("\nrRRRRRRRRRRetorna "+c.getCliNombre());				
				return c;
				
			}
		}	
		System.out.println("Reeetorna null");				
		
		return null;	
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {	
		System.out.println("Convirtiendo a "+((Cliente)value).getCliCedula());
		return ((Cliente)value).getCliCedula();
	}
	
	public List<Cliente> getDAO(){				
		FacesContext ctx = FacesContext.getCurrentInstance();
		ELContext ec = ctx.getELContext();
		Application app = ctx.getApplication();
		return (List) app.evaluateExpressionGet(ctx,"#{asignarUsuarioOnline.listaClientes}",List.class);		
	}
}
